package com.lzy.business.controller.agent;


import com.github.pagehelper.PageInfo;
import com.lzy.business.controller.BaseController;
import com.lzy.business.facade.AgencyManagementFacade;
import com.lzy.business.request.AgencyManagementRequest;
import com.lzy.business.response.AgencyManagementReponse;
import com.lzy.business.response.AgentProtocolCommissionDtoReponse;
import com.lzy.framework.component.PageBean;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.product.apis.AgentProtocolCommissionServiceApi;
import com.lzy.product.apis.ProductUserServiceApi;
import com.lzy.product.common.util.UserUtil;
import com.lzy.product.dtos.ProductUserDto;
import com.lzy.product.dtos.agent.AgentProtocolCommissionDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/admin/agent/manage")
public class AgencyManagementController extends BaseController {


    @Autowired
    private AgencyManagementFacade agencyManagementFacade;

    @Autowired
    private ProductUserServiceApi productUserServiceApi;

    @Autowired
    private AgentProtocolCommissionServiceApi agentProtocolCommissionServiceApi;

    @Value("${share_uri}")
    String share_uri;

    @ResponseBody
    /*@RequiresPermissions("sys:agent:addAgent")*/
    @RequestMapping(value = "/addAgent", method = RequestMethod.POST)
    public Result addAgent(ProductUserDto userDto) {
        Long loginUserId = UserUtil.getCurrentUser().getUserId();
        ProductUserDto productUserDto = agencyManagementFacade.addAgent(userDto, loginUserId);
        return new Result(productUserDto);
    }

    @RequestMapping(value = "/querySysByProtocol/{protocolId}")
    @ResponseBody
    public Result querySysByProtocol(@PathVariable("protocolId") Long protocolId) {
        List<AgentProtocolCommissionDto> agentProtocolCommissionDtoList = agentProtocolCommissionServiceApi.selectByProtocolId(protocolId);
        return new Result(DozerUtils.maps(agentProtocolCommissionDtoList, AgentProtocolCommissionDtoReponse.class));
    }

    @ResponseBody
    @RequestMapping(value = "/json", method = RequestMethod.GET)
    public PageBean json(AgencyManagementRequest request) {
        PageInfo pageInfo = agencyManagementFacade.searchAgents(request);
        return new PageBean<AgencyManagementReponse>(pageInfo.getTotal(), pageInfo.getList());
    }

    @ResponseBody
    @RequestMapping(value = "/searchChilds", method = RequestMethod.GET)
    public Result searchChilds(Long id) {
        return new Result(agencyManagementFacade.searchChilds(id));
    }

    @ResponseBody
    @RequestMapping(value = "/queryAgency", method = RequestMethod.GET)
    public Result queryAgency(Long userId) {
        return new Result(agencyManagementFacade.queryAgency(userId));
    }


    @ResponseBody
    @RequestMapping("/queryCity/{type}/{isAgent}")
    public Result queryCity(@PathVariable("type") Integer type, @RequestParam("cityCode") Integer cityCode, @PathVariable("isAgent") Integer isAgent) {
        return new Result(agencyManagementFacade.queryCity(type, cityCode, isAgent));
    }

    /**
     * 更新会员状态
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/updateStats/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Result updateStatus(@PathVariable("id") Long id) {
        getCurrentUser();
        ProductUserDto productUserDto = productUserServiceApi.findById(id);
        Ensure.that(productUserDto.getAgentNumber() != null && productUserDto.getSystemId() != null).isTrue("01001621");
        Ensure.that(productUserDto).isNotNull("01001505");
        Integer status = productUserDto.getState() == null ? 0 : productUserDto.getState();
        //如果当前状态为0 则改为1 为1 则改为0 互斥操作
        if (status.equals(1)) {
            productUserDto.setState(0);
        } else {
            productUserDto.setState(1);
        }
        Ensure.that(productUserServiceApi.update(productUserDto)).isTrue("01001620");
        return new Result(productUserDto);
    }






    @RequestMapping("/searchAgents")
    public PageBean searchAgents() {
        return null;
    }

    @RequestMapping("/toagentAdd")
    public String toagentAdd() {
        return "/agent/manage/agent_add";
    }

    @RequestMapping("/toagentAddView")
    public String toagentAddView() {
        return "/agent/manage/agent_add_view";
    }

    @RequestMapping("")
    public String toagentList(Model model) {
        model.addAttribute("share_uri",share_uri);
        return "/agent/manage/agent_list";
    }

    @RequestMapping("/toagentView")
    public String toagentView() {
        return "/agent/manage/agent_view";
    }

    @RequestMapping("/tounderLevelView")
    public String tounderLevelView() {
        return "/agent/manage/underLevel_view";
    }




}
